//
//  getAllSchoolList.swift
//  压岁_IOS
//
//  Created by 梓简王 on 2017/8/2.
//  Copyright © 2017年 梓简王. All rights reserved.
//

import Foundation
import Alamofire
import QorumLogs

class getAllSchoolList {
    static let PROVINCE_URL = "http://www.hisihi.com/app.php?s=/school/province"
    static let SCHOOL_URL = "http://www.hisihi.com/app.php?s=/school/school/provinceid/"
    static var proDis:NSMutableArray = NSMutableArray()
    static func getPROVINCE(){
        Alamofire.request(.GET, PROVINCE_URL,parameters:nil,headers:nil).responseJSON() {
            response in
            guard let JSONDIC = response.result.value else {
                QL4(response.result.error)
                switch response.result.error!.code {
                case -1009 :
                    SweetAlert().showAlert("对不起", subTitle: "请检查您的网络配置", style: AlertStyle.Error)
                case 3840:
                    SweetAlert().showAlert("对不起", subTitle: "服务出现故障，请联系管理员", style: AlertStyle.Error)
                case -1001 :
                    
                    SweetAlert().showAlert("对不起", subTitle: "上传信息超时", style: AlertStyle.Error)
                default:
                    SweetAlert().showAlert("对不起", subTitle: "请检查您的网络配置", style: AlertStyle.Error)
                }
                return }
//            QL2(JSONDIC)
            let resultDict = JSONDIC as? NSDictionary
            let jsonObj = JSON(JSONDIC)
            let errorCode = jsonObj["error_code"].intValue
            let errorMsg = jsonObj["message"].stringValue
//            QL2(errorMsg)
            let provinceList = jsonObj["data"].arrayValue
            for item in provinceList {
                getSchoolList(item)
            }
        }
    }
    static func getSchoolList(provinceInfo:JSON){
        let url:String = "\(SCHOOL_URL)\((provinceInfo.dictionaryValue["province_id"]?.stringValue)!)"
        QL1(url)
        Alamofire.request(.GET, url,parameters:nil,headers:nil).responseJSON() {
            response in
            guard let JSONDIC = response.result.value else {
                QL4(response.result.error)
                switch response.result.error!.code {
                case -1009 :
                    SweetAlert().showAlert("对不起", subTitle: "请检查您的网络配置", style: AlertStyle.Error)
                case 3840:
                    SweetAlert().showAlert("对不起", subTitle: "服务出现故障，请联系管理员", style: AlertStyle.Error)
                case -1001 :
                    
                    SweetAlert().showAlert("对不起", subTitle: "上传信息超时", style: AlertStyle.Error)
                default:
                    SweetAlert().showAlert("对不起", subTitle: "请检查您的网络配置", style: AlertStyle.Error)
                }
                return }
//            QL2(JSONDIC)
            let resultDict = JSONDIC as? NSDictionary
            let jsonObj = JSON(JSONDIC)
            let errorCode = jsonObj["error_code"].intValue
            let errorMsg = jsonObj["message"].stringValue
            let schoolData = jsonObj["data"].arrayValue
            var schoolArr = NSMutableArray()
            var rootDic = NSMutableDictionary()
            for item in schoolData {
                //Key
                schoolArr.addObject((item.dictionaryValue["school_name"]?.stringValue)!)
            }
            rootDic["area"] = (provinceInfo.dictionaryValue["province_name"]?.stringValue)!
            rootDic["schoolArr"] = schoolArr
            proDis.addObject(rootDic)
//            proDis["\((provinceInfo.dictionaryValue["province_name"]?.stringValue)!)"] = schoolArr
            if( provinceInfo.dictionaryValue["province_id"]?.stringValue == "34"){
                proDis.writeToFile(commonVariable.SCHOOLINFOPATH, atomically: true)
                QL1(commonVariable.SCHOOLINFOPATH)
            }
        }
    }
}